import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import update from 'react-addons-update';
import { Toast } from 'antd-mobile';
import Utils from '../../../utils/index'
import DB from '../../../app/db';
import nattyStorage from 'natty-storage';
import StorageUtils from '../../../app/storage'
import { Control } from 'react-keeper'
import {updUserDoors} from "../toupiao/const/Const";

const storageUser = nattyStorage({
  type: 'localStorage',
  key: 'sskj-wechat',
});

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    userInfo: {},
    doors: []
  },

  init() {
    // this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getUser()}});
    // this.updateComponent();
  },

  initMode: function () {
    let t = this;
    t.data.userInfo = update(t.data.userInfo, {$merge: {...StorageUtils.getUser()}});
    t.updateComponent();
    t.updateDoors();
  },

  updateDoors() {
    let t = this;
    DB.WechatAPI.getInfoByOpenId({
      openId: t.data.userInfo.openId
    }).then(
      function (content) {
        if (content.length > 0) {
          StorageUtils.setStorage('doors', content);
          t.data.doors = update(t.data.doors, {$set: content});
          t.updateComponent();
        }
      },
      function (error) {
        console.log(error);
      }
    );
  },

  onCaseOk: function (e) {
    let t = this;
    DB.WechatAPI.bindCastok({'doorId': e}).then(
      function (content) {
        // 重新获取门牌号信息
        t.updateDoors();
        Toast.success('报名成功');
      },
      function (error) {
        console.log(error);
      });
  },

  onCaseNo: function (e) {
    let t = this;

    DB.WechatAPI.bindCastno({'doorId':e}).then(
      function (content) {
        t.data.userInfo.doors.forEach(function(value,index,array){
          if(value.id===e)
          {
            value.castUnit=false;
          }
        });
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });
  },

  onEnterInfo: function (e) {
    let t = this;
    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});
    console.info('未查找到数据',this.data.userInfo );
    if(this.data.userInfo.id !== 'xxx')
    {

      DB.WechatAPI.loadDoorBind({
        proid: this.data.userInfo.id,
      }).then(
        function (content) {
          console.info(content);
          let resultObj = Utils.isEmptyObject(content);

          console.info(resultObj.success);
          if (!resultObj.success) {

          }
          else
          {
            if (content.length <= 0) {
              console.info('未查找到数据');
            }
            else
            {
              t.data.doors = update(t.data.doors, {$set: content});
              t.data.userInfo = update(t.data.userInfo, {doors: {$set: content}});
              t.data.userInfo = update(t.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});

              t.updateComponent();

            }
            console.info('门牌号');
            console.info(t.data.userInfo);
          };

        },
        function (error) {
          console.log(error);
        });

    }

  },

  getXiaoqu() {
    let t = this;
    DB.ywh.getXiaoqu({proid: t.data.userInfo.proprietorId}).then(
      function (content) {
        if (content && content.length > 0) {
          let xq = [];
          content.map(d => {
            xq.push(d);
          });
          t.data.xq = update(t.data.xq, {$set: xq});
          t.updateComponent();
        }
      },
      function (error) {
        console.log(error);
      }
    );
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});
